/*
 * 分享朋友圈插件
 * 依赖的5+模块列表
 * Downloader
 * IO
 * Native.js
 * */
(function() {
	var wxShare = {
		share: function(type, imgs, text) {
			var self = this;
			this._down(imgs, function(paths) {
				self._share(type, paths, text);
			});
		},
		_down: function(imgs, callback) {
			var paths = [];
			mui.plusReady(function() {
				plus.nativeUI.showWaiting('获取图片...');
				imgs.forEach(function(imgUrl) {
					plus.downloader.createDownload(imgUrl, {
						method: "GET",
						filename: '_downloads/image/' + md5(imgUrl)+'.jpg',
						timeout: 10,
					}, function(download, status) {
						if(status === 200) {
							paths.push(plus.io.convertLocalFileSystemURL(download.filename));
							if(paths.length === imgs.length) {
								callback(paths);
								plus.nativeUI.closeWaiting();
							}
						} else {
							new Error(imgUrl + 'download error');
						}
					}).start();
				});
			});
		},
		_share: function(type, paths, text) {
			var Intent = plus.android.importClass('android.content.Intent');
			var ComponentName = plus.android.importClass('android.content.ComponentName');
			var ArrayList = plus.android.importClass('java.util.ArrayList');
			var Uri = plus.android.importClass('android.net.Uri');
			var File = plus.android.importClass('java.io.File');
			var intent = new Intent();
			var localComponentName;
			var localArrayList = new ArrayList();
			var act = plus.android.runtimeMainActivity();
			if(type === 'timeLine') {
				localComponentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
			} else {
				localComponentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
			}
			intent.setComponent(localComponentName);
			intent.setAction("android.intent.action.SEND_MULTIPLE");
			intent.setType("image/*");
			paths.forEach(function(filePath) {
				localArrayList.add(Uri.fromFile(new File(filePath)));
			});
			intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
			if(typeof text === 'string' && text.length) {
				intent.putExtra("Kdescription", text);
			}
			act.startActivity(intent);
		},
	}
	window.wxShare = wxShare;
})();

// 使用示例
//朋友圈
// wxShare.share('timeLine',['https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'],'test');
//好友
// wxShare.share('friend',['https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png','https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png'],'test');